#region using
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using BLToolkit.Mapping;
using BLToolkit.Data;
using BLToolkit.DataAccess;
using BLToolkit.EditableObjects;
#endregion

namespace Brainsoft.Core.BusinessLogic
{
    /// <summary>
    /// Este objeto representa las propiedades y metodos de ApplicationItemsRole.
    /// Nomenclatura a usar para los SP = ApplicationItemsRole_SelectByKey , ApplicationItemsRole_Update, etc.
    /// En caso contrario usar el nombre de los SP como atributos de la clase, ej:
    ///[ActionSprocName("SelectAll", "NombreProcedimiento")], [ActionSprocName("Insert", "NombreProcedimiento")], etc.
    /// </summary>	

    public class ApplicationItemsRole
    {

        #region Constructors
        public ApplicationItemsRole()
        {
        }
        #endregion

        #region Private Properties
        private int _roleId;
        private int _applicationItemID;
        #endregion

        #region Public Properties
        public int RoleId
        {
            get { return _roleId; }
            set { _roleId = value; }
        }

        public int ApplicationItemID
        {
            get { return _applicationItemID; }
            set { _applicationItemID = value; }
        }
        #endregion

        #region Relational Properties

        #endregion

        #region Generic Methods

        #region Insert, Update, Delete

        public void Insert(ApplicationItemsRole obj)
        {
            Query.Insert(db, obj);
        }

        public void Update(ApplicationItemsRole obj)
        {
            Query.Update(db, obj);
        }

        public void Delete(ApplicationItemsRole obj)
        {
            Query.Delete(db, obj);
        }

        public void Delete(int id)
        {
            Query.DeleteByKey(db, id);
        }

        #endregion

        #region Select

        public List<ApplicationItemsRole> SelectAll()
        {
            return Query.SelectAll(db);
        }

        public ApplicationItemsRole SelectByKey(object[] parameters)
        {
            return Query.SelectByKey(db, parameters);
        }

        #endregion

        #region Batch
        public bool BatchInsert(List<ApplicationItemsRole> list)
        {
            using (db = new DbManager())
            {
                db.BeginTransaction();
                try
                {
                    foreach (ApplicationItemsRole obj in list)
                    {
                        Query.Insert(db, obj);                        
                    }
                }
                catch 
                {
                    //new BrainsoftExceptions.Handler(ex, BrainsoftExceptions.ErrorPolicy.ErrorCapaDatos);
                    db.RollbackTransaction();
                    return false;
                }
                db.CommitTransaction();
                return true;
            }
        }
        public bool BatchUpdate(List<ApplicationItemsRole> list)
        {
            using (DbManager db = new DbManager())
            {
                db.BeginTransaction();
                try
                {
                    foreach (ApplicationItemsRole obj in list)
                    {
                        obj.Update(obj);
                    }
                }
                catch
                {
                    db.RollbackTransaction();
                    return false;
                }
                db.CommitTransaction();
                return true;
            }
        }
        #endregion

        #endregion

        #region Custom Methods
        public List<ApplicationItemsRole> GetByRol(int RolId)
        {
            using (db = new DbManager("Connection String"))
            {
                return db.SetSpCommand("ApplicationItemsRoles_SelectByRolId"
                    , db.Parameter("@RolId", RolId))
                    .ExecuteList<ApplicationItemsRole>();
            }            
        }
               
        public List<ApplicationItemsRole> GetByItemId(int ApplicationItemId)
        {
            using (db = new DbManager("Connection String"))
            {
                return db.SetSpCommand("ApplicationItemsRoles_SelectByApplicationItemId"
                    , db.Parameter("@ApplicationItemId", ApplicationItemId))
                    .ExecuteList<ApplicationItemsRole>();
            }            
        }
        public void DeleteByApplicationItemId(int ApplicationItemId)
        {
            using (db = new DbManager("Connection String"))
            {
                db.SetSpCommand("ApplicationItemsRoles_DeleteByApplicationItemId"
                    , db.Parameter("@ApplicationItemId", ApplicationItemId))
                    .ExecuteNonQuery();
            }
        }  
        #endregion

        #region Protected Members

        protected DbManager db;

        private SprocQuery<ApplicationItemsRole> _query;
        protected virtual SprocQuery<ApplicationItemsRole> Query
        {
            get
            {
                if (null == _query)
                    db = new DbManager("Connection String");//ingresar aqu? el Connection String
                _query = new SprocQuery<ApplicationItemsRole>(db);
                return _query;
            }
        }

        #endregion
    }
}
